<template>
  <div class="home-container">
    <el-container class="el-container-box">
      <el-header>
        <div class="header-left">
          <img src="../assets/logo.png" alt="">
          <h3>后台管理系统</h3>
        </div>
        <el-button @click="logout">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :width="flag ? '60px' : '200px'">
          <div class="toggle-width" @click="flag = !flag">|||</div>
          <el-menu router :default-active="defaultActive" :collapse="flag" :collapse-transition="false" unique-opened
            background-color="#545c64" text-color="#fff" class="el-menu-vertical-demo">
            <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
              <template slot="title">
                <i :class="menuObj[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <el-menu-item @click="setDefaultActive('/' + item2.path)" :index="'/' + item2.path"
                v-for="item2 in item.children" :key="item2.id">
                <i class="el-icon-menu"></i>
                <span slot="title"> {{item2.authName}}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        flag: false,
        defaultActive: '',
        menuList: [],
        menuObj: {
          125: 'iconfont icon-users',
          103: 'iconfont icon-tijikongjian',
          101: 'iconfont icon-danju',
          102: 'iconfont icon-shangpin',
          145: 'iconfont icon-baobiao'
        }
      }
    },
    created() {
      this.getMenuList()
    },
    methods: {
      logout() {
        sessionStorage.removeItem('token')
        this.$router.push('/login')
      },
      setDefaultActive(path) {
        sessionStorage.setItem('defaultActive', path)
        this.defaultActive = path
      },
      async getMenuList() {
        const {
          data: result
        } = await this.$http.get('/menus')
        this.menuList = result.data
        this.defaultActive = sessionStorage.getItem('defaultActive') || ''
      }
    }

  }
</script>

<style lang="less" scoped>
  .home-container {
    height: 100%;

    .el-container-box {
      height: 100%;
    }

    .el-menu-vertical-demo {
      border-right: 0;
    }

    .toggle-width {
      text-align: center;
      color: #fff;
      background-color: #545C75;
      font-size: 12px;
      letter-spacing: 0.2em;
      height: 25px;
      line-height: 25px;
      cursor: pointer;
      user-select: none;
    }

    .el-submenu__title i {
      margin-right: 10px;
    }

    .el-main {
      background-color: #fafafa;
    }

    .el-header {
      user-select: none;
      background-color: #333;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .header-left {
        display: flex;
        justify-content: space-between;
        align-items: center;
        color: #fff;
        font-size: 23px;

        img {
          width: 50px;
          height: 50px;
          margin-right: 20px;
        }
      }
    }

    .el-aside {
      background-color: #545c64;
    }
  }
</style>